package org.testis.data.parser;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.testdriver.AtofEnumaration;
import org.testis.testdriver.TestPlanGlobalsHashMap;
import org.testis.testdriver.TestStepGlobalDataHashMap;
import org.testis.testdriver.translation.AtofValueTranslateService;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;


/**
 *
 * @author nurhat
 */
public class WAMLanguageSelectionDopDecorator extends WAMDopDecorator
{

    public WAMLanguageSelectionDopDecorator(
        WebAutomationModuleDataObject data,
        DynamicMapDesignTreeNode root)
    {
        super(data,
              root);
    }


    @Override
    protected boolean setBehavior()
    {
        String linkAbstractId = getData().getDataToApply().get(
            AtofModuleParameterNames.WEB_AUTO_ABSTRACT_LINK_ID);
        if (linkAbstractId == null) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "No parameter given for LanguageSelectionAction",
                getData().getMessageId());
            return false;
        }
        try {
            AtofValueTranslateService service = new AtofValueTranslateService(
                    TestPlanGlobalsHashMap.createInstance().getTestplanglobals(),
                    TestPlanGlobalsHashMap.createInstance().getTestplanlanguages(),
                    TestPlanGlobalsHashMap.createInstance().getTestplandutvalues(),
                    TestStepGlobalDataHashMap.createInstance().getTeststepglobals());

            linkAbstractId =
                service.translateValue(linkAbstractId);
        } catch (Exception ex) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                linkAbstractId +
                " not found in global or language variables. Error:" + ex.
                getMessage(),
                getData().getMessageId());
            return false;
        }
        getData().getDataToApply().clear();
        DynamicMapDesignTreeNode link = findObject(linkAbstractId);

        if (link == null) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "Given link not found in map at LanguageSelectionAction. ",
                getData().getMessageId());
            return false;
        }

        getData().setActionType(AtofEnumaration.AtofWebAutomationModuleAction.Set.
                                name());
        getData().getDataToApply().clear();

        getData().getDataToApply().put(linkAbstractId,
                                       "");
        return true;
    }
}
